
local excel = load_excel('挑战设定.xlsx','随机事件')


for i,v in pairs(excel) do
    local name = v['事件名称']
    v.name = name
    v.tip = v['随机事件介绍']
    ac.lni('skill',name,v)

    local mt = ac.skill[name]
    function mt:on_cast_shot()
        local name = self.name
        local unit = self.owner
        local player = unit.owner
        local hero = player.hero
        player:send_msg(('触发随机事件[%s]'):format(name))
        player:send_msg(self.tip)
        player:notify('玩家-开始随机事件',player)
        if name=='疯狂星期四' then
            hero:add_item('麦辣鸡块',{charges = self.data1})
        elseif name=='树人入侵' then
            ac.timer(10/self.data1 * 1000,self.data1,function()
                local unit = player:add_enemy('杀敌怪',true)
                unit:set_model([[units\creeps\CorruptedEnt\CorruptedEnt.mdl]])
                unit.ui_title = '树人'
                unit['木材'] = math.random(self.data2,self.data3)
                unit:set_enemy_level(ac.enemy.day)
            end)
        elseif name == '金猪入侵' then
            ac.timer(10/self.data1 * 1000,self.data1,function()
                local unit = player:add_enemy('杀敌怪',true)
                unit:set_size(0.5)
                unit:set_model([[4554033638daa1d2abeb9cc9b71dbf38.mdl]])
                unit.ui_title = '小金猪'
                unit['金币'] = math.random(self.data2,self.data3)
                unit:set_enemy_level(ac.enemy.day)
            end)
        elseif name =='土匪入侵' then
            ac.timer(10/self.data1 * 1000,self.data1,function()
                local unit = player:add_enemy('杀敌怪',true)
                unit:set_model([[units\creeps\Bandit\Bandit.mdl]])
                unit.ui_title = '土匪'
                unit['杀敌'] = math.random(self.data2,self.data3)
                unit:set_enemy_level(ac.enemy.day)
            end)
        elseif name == '武神附体' then
            hero:add_buff(name){
                time = 60,
                art = self.art,
                keep = 1,
                on_add = function(buff)
                    buff:gc(hero:add('全属性加成',50))
                end
            }
        elseif name == '魔力膨胀' then
            hero:add('魔法恢复',5)
            hero:add_buff(name){
                time = 30,
                art = self.art,
                keep = 1,
                on_add = function(buff)
                    buff:gc(hero:add('魔法恢复',10))
                    buff:gc(hero:event '单位-受到伤害前'(function(_,_,data)
                        data:mul(25)
                    end))
                end
            }
        elseif name == '拔苗助长' then
            for a=1,math.random(3,6) do
                hero:notify('单位-添加词条选择',hero)
            end
        elseif name == '杀戮盛宴' then
            hero:add_buff(name){
                time = 30,
                art = self.art,
                keep = 1,
                on_add = function(buff)
                    buff:gc(hero:add('攻击速度',self.data1))
                    buff:gc(hero:add('杀敌攻击',self.data2))
                    buff:gc(hero:add('杀敌力量',self.data2))
                    buff:gc(hero:add('杀敌敏捷',self.data2))
                    buff:gc(hero:add('杀敌智力',self.data2))
                end
            }
        elseif name == '☆购物节狂欢☆' then
            local unit = player:get_data('孤岛商人')
            if unit:find_buff '未解锁' then
                unit:remove_buff '未解锁'
                return
            end
            ac.lock_shop = 5
            unit.update_timer:on_timer()
            ac.lock_shop = nil
        elseif name == '☆唤醒龙族☆' then
            local units = {'远古龙皇'}
            local count = 11
            for a=1,count do
                local unit = player:add_enemy(units[a] or '幼龙',true)
                unit:set_enemy_level(unit['默认等级'] or ac.enemy.day)
                unit:event '单位-死亡'(function()
                    count = count - 1
                    if count==0 then
                        hero:add_item('龙蛋')
                    end
                end)
            end
            
        elseif name == '☆火力补给☆' then
            local unit = player:get_data('炮台')
            if unit:find_buff '未解锁' then
                unit:remove_buff '未解锁'
                return
            end
            player:notify('玩家-添加炮弹',player,self.data1)
        elseif name == '☆金蟾狂欢☆' then
            for a=1,self.data1 do
                local unit = player:add_enemy('金蟾',true)
                unit:set_enemy_level(ac.enemy.day)
            end
        end
    end
end

local mt = ac.skill['随机选择事件']
mt.tip = [[随机获取另一个事件]]
mt.art = [[ReplaceableTextures\CommandButtons\BTNSelectHeroOn.blp]]
function mt:on_cast_shot()
    local unit = self.owner
    local player = unit.owner
    local hero = player.hero
    local group = {}
    for _,temp in ipairs(excel) do
        if temp.name~=unit.lock_event then
            table.insert(group,temp.name)
        end
    end
    local name = table.random(group)
    local skill = unit:add_skill(name,'英雄')
    skill:on_cast_shot()
end

ac.game:event '玩家-开始随机事件'(function(_,player)
    local unit = player:get_data('随机事件')
    unit:remove_skills()
    unit.update_timer = ac.wait(unit:get('生命上限') * 1000,function()
        unit.update_timer = nil
        player:notify('玩家-刷新随机事件',player)
    end)
    
    unit:loop(1 * 1000,function(t)
        if unit.update_timer then
            local time = unit.update_timer:get_remaining()/1000
            unit:set('生命',unit:get('生命上限') - time + 0.1)
        else
            t:remove()
            unit:set('生命',unit:get('生命上限'))
        end
    end)
end)

ac.game:event '玩家-刷新随机事件'(function(_,player)
    local unit = player:get_data('随机事件')
    local data = table.random(excel)
    unit.lock_event = data.name
    
    unit:remove_skills()
    unit:add_skill(unit.lock_event,'英雄')
    unit:add_skill('随机选择事件','英雄')
    player:notify('随机事件',player,unit)
end)

ac.game:event '英雄-击杀单位'(function(_,hero,unit)
    local player = hero.owner
    local shop = player:get_data('随机事件')
    if shop:find_buff '未解锁' or unit:is_type('精英')==false then
        return
    end
    local point = unit:get_point()
    
    point:bezier_missile(shop,{math.random(90,150),500},[[Abilities\Weapons\FaerieDragonMissile\FaerieDragonMissile.mdl]],1200,function()
        if shop.update_timer then
            local func = shop.update_timer.on_timer
            local time = shop.update_timer:get_remaining() - 1*1000
            shop.update_timer:remove()
            shop.update_timer = ac.wait(time,func)
        end
    end)
end)